package domain.web.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import process.model.AnnotatedPage;
import process.model.MenuNode;
import process.model.ProcessResult;
import process.model.event.AnnotatedPageEvent;
import process.model.lookup.ProductLookup;
import process.service.AnnieProcessService;
import process.service.MenuParserService;
import process.service.OntoQueryService;
import domain.web.model.StoreView;
import domain.web.model.rq.ParsePageRq;
import domain.web.service.ProductService;
import domain.web.service.StoreService;

@Controller
@RequestMapping("admintool")
public class AdminController {

	@Autowired
	private AnnieProcessService annieProcessService;
	@Autowired
	private OntoQueryService ontoQueryService;
	@Autowired
	private StoreService storeService;
	@Autowired
	private MenuParserService menuParserService;
	@Autowired
	private ProductService productService;

	// ------------------------ PARSE A SINGLE PAGE ------------------
	// Get Parse page Request template
	@RequestMapping(value = "/page/pagerq", method = RequestMethod.POST)
	@ResponseBody
	public ParsePageRq getPageRqTemplate() {
		return new ParsePageRq();
	}

	@RequestMapping(value = "/page/parse", method = RequestMethod.POST)
	@ResponseBody
	public ProcessResult getPageResult(@RequestBody ParsePageRq parsePageRq)
			throws Exception {
		ontoQueryService.setDefinedNews(new ArrayList<ProductLookup>());
		ontoQueryService.setUnDefinedNews(new ArrayList<ProductLookup>());
		ontoQueryService.setUpdates(new ArrayList<ProductLookup>());

		List<AnnotatedPage> annotatedPages = new ArrayList<AnnotatedPage>();
		try {
			File input = new File(parsePageRq.getLink());
			Document doc = Jsoup.parse(input, "UTF-8");
			annotatedPages.add(annieProcessService.annotateDoc(doc));
		} catch (FileNotFoundException ex) {
			try {
				annotatedPages.add(annieProcessService.annotate(parsePageRq
						.getLink()));
			} catch (Exception e) {
				return null;
			}
		}

		AnnotatedPageEvent event = new AnnotatedPageEvent(annotatedPages,
				new MenuNode(parsePageRq.getMenu()), parsePageRq.getStoreId());
		ontoQueryService.handleAnnotatedPageEvent(event);
		return ontoQueryService.getResult();
	}

	@RequestMapping(value = "/site/parse", method = RequestMethod.POST)
	@ResponseBody
	public ProcessResult getSiteResult(@RequestBody ParsePageRq parsePageRq)
			throws Exception {
		ontoQueryService.setDefinedNews(new ArrayList<ProductLookup>());
		ontoQueryService.setUnDefinedNews(new ArrayList<ProductLookup>());
		ontoQueryService.setUpdates(new ArrayList<ProductLookup>());

		StoreView storeView = storeService.getView(parsePageRq.getStoreId());

		menuParserService.parse(storeView.getId(), storeView.getWebsite(),
				storeView.getMenuQ(), storeView.getPaginatorQ());
		return ontoQueryService.getResult();
	}

	@RequestMapping(value = "/insertproduct/{storeId}", method = RequestMethod.POST)
	public void insertProduct(@PathVariable("storeId") long storeId,
			@RequestBody ProductLookup lookup) throws Exception {
		productService.insert(lookup, storeId);
	}
}
